import Taro, { Component } from '@tarojs/taro'
import { AtTabs, AtTabsPane, AtButton } from 'taro-ui'
import { View, ScrollView } from '@tarojs/components'
import { DNavBar, Ddraworder, DModal, DItemError, DPaymodel, DAtInpute } from '@/components'
import './drawback.scss'
import http from '@/api'
const getUrlScheme = http.getUrlScheme
export default class Shoporders extends Component {

  constructor(props) {
    super(props)
    this.state = {
      active: 0,
      isShow: false,
      scrollStyle: window.screen.availHeight,
      footerStatus: false,
      isOpened: false,
      obj1: {
        page: 1,
        limit: 10,
        orStage: 1
      },
      searchVal: '',
      item: {},
      indList: {},
      totalPage: 1,
      list: [],
      creatorId: ""
    }
  }

  componentWillMount() {
    let { active = 0 } = this.$router.params
    if (window.localStorage.getItem('ISPAY')) {
      // this.state.isOpened： = true
      this.setState({
        isOpened: true
      }, () => window.localStorage.removeItem('ISPAY'))
    }
    this.setState({
      active: +active
    })
    this.getSaleOrder(+active)
  }

  componentDidShow() {
    let { obj1, active } = this.state
    if (+active === 0) {
      obj1.page = 1
      obj1.orStage = 1
    } else if (+active === 1) {
      obj1.page = 1
      obj1.orStage = 2
    } else if (+active === 2) {
      obj1.page = 1
      obj1.orStage = 3
    } else if (+active === 3) {
      obj1.page = 1
      obj1.orStage = 4
    }
    this.setState({
      obj1,
      list: [],
      footerStatus: false
    }, () => {
      this.getSaleOrder()
    })
  }
  getSaleOrder(active) {
    let { page, limit } = this.state.obj1
    let { totalPage } = this.state
    let creatorId = ''
    if (localStorage.getItem('userInformation')) {
      let { dsfUserEntity = '' } = JSON.parse(localStorage.getItem('userInformation'))
      creatorId = dsfUserEntity.userId
    }
    let obj = {
      page,
      limit,
      orderNo: this.state.searchVal
      // creatorId: creatorId,//订单买方id
      // orStage: active ? +active + 1 : orStage  //tab状态
    }
    if (page > totalPage) {
      return this.setState({
        footerStatus: true
      })
    }

    http.tradingRefund(obj).then(res => {
      let _list = []
      if (res.list && res.list.length > 0) {
        let { list } = this.state

        if (page > 1) {
          _list = [...list, ...res.list]
        } else {
          _list = [...res.list]
        }

      }
      this.setState({
        list: _list,
        totalPage: res.totalPage || 1
      })
    })
  }
  getStatus(status) {
    return {
      $11: `待支付`,
      $21: `待卖家接单`,
      $31: `待卖家上传`,
      $32: `待确认`,
      $33: `待核实`,
      $41: `交易成功`,
      $42: `拒绝接单`,
      $43: `已撤回`,
      $44: `已超时`,
      $45: `交易异常`,
      $49: `待退款`,
    }[`$${status}`]
  }
  handleClick(value) {
    let { obj1 } = this.state
    this.setState({
      list: [],
      footerStatus: false
    })
    // ,()=>{
    if (value === 0) {
      this.setState({
        obj1: {
          ...obj1,
          page: 1,
          orStage: 1
        }
      }, () => this.getSaleOrder())
    } else
      if (value === 1) {
        this.setState({
          obj1: {
            ...obj1,
            page: 1,
            orStage: 2
          }
        }, () => this.getSaleOrder())
      } else
        if (value === 2) {
          this.setState({
            obj1: {
              ...obj1,
              page: 1,
              orStage: 3
            }
          }, () => this.getSaleOrder())
        } else
          if (value === 3) {
            this.setState({
              obj1: {
                ...obj1,
                page: 1,
                orStage: 4
              }
            }, () => this.getSaleOrder())
          }
    this.setState({
      active: value
    })
    // }
  }
  onScroll = () => {
    this.setState({
      obj1: {
        ...this.state.obj1,
        page: this.state.obj1.page + 1
      }
    }, () => {
      this.getSaleOrder()
    })
  }
  //详情跳转
  handlerprops(item) {
    let { trId: id, refundStatus } = item
    Taro.navigateTo({
      url: `/pages/itemdetail/drawdetail?id=${id}&status=${refundStatus}`
    })
  }

  onHandleFirst() {
  }
  //确认合同
  onhandleContract(item) {
    Taro.navigateTo({
      url: `/pages/contractproof/contractproof?id=${item.id}`
    })
  }
  //撤回订单
  onhandleRecall(item) {
    let isShow = !isShow
    this.setState({
      isShow,
      item
    })
  }
  //关闭模态框
  onClose() {
    let isShow = false
    this.setState({
      isShow
    })
  }
  //撤回订单
  onLastHandle() {
    let { item } = this.state
    this.onClose()
    this.drawOrder()
  }

  drawOrder() {
    let { item } = this.state
    http.drawOrder({ orderId: `${item.id}` }).then(res => {
      Taro.navigateTo({
        url: `/pages/itemdetail/shopdetail?id=${item.id}&status=43`
      })
    })
  }
  onCancelPay(payType = '1') {
    let { indList } = this.state
    http.transCaPayOrder({ id: indList.id, payIp: window.payIp, payType }).then(res => {
      if (payType == 8) {
        console.log(res);
        return
      }
      setTimeout(() => {
        // Taro.navigateBack({
        //     delta: 2
        // })
        window.localStorage.setItem('ISPAY', 1)
        if (!res.payUrl) {
          return Taro.showToast({
            title: '订单状态异常',
            icon: 'none'
          })
        }
        if (payType == 7) {
          let url = encodeURIComponent(res.payUrl)
          return Taro.navigateTo({
            url: `/pages/wxQrCode?payUrl=${url}`
          })
        }

        window.location.href = res.payUrl

      }, 1000)
    })

  }

  onHandleOrNo(searchVal) {

    this.setState({
      searchVal
    })
  }
  searchList() {
    let obj1 = { ...this.state.obj1, page: 1 }
    this.setState({
      obj1
    }, () => this.getSaleOrder())

  }
  render() {
    const {
      active,
      list,
      isShow,
      scrollStyle,
      footerStatus,
      searchVal
    } = this.state
    return (
      <View className='b_drawback'>
        <DNavBar title={'退款'} className='b_drawback_nav'></DNavBar>
        <View className='b_drawback__box'>
          <View className='b_drawback__box_search flex-box'>
            <DAtInpute onChange={this.onHandleOrNo.bind(this)} value={searchVal} placeholder="请输入订单号"></DAtInpute>
            <AtButton size="small" onClick={this.searchList.bind(this)} type='secondary' >搜索</AtButton>
          </View>
          <View className='b_drawback__content'>

            <ScrollView
              scrollY
              scrollWithAnimation
              lowerThreshold='20'
              style={`height:${scrollStyle - 140}px`}
              onScrollToLower={this.onScroll}>
              {
                list && list.map((item, i) => {
                  return <Ddraworder
                    key={i}
                    order={item.businessOrderNo}
                    status={this.getStatus(item.refundStatus)}
                    list={[item.comName]}
                    flag={[item.orCarSourceType, item.orCarEmissionStd, '外观颜色:', item.orCarOutColor]}
                    money={item.refundAmount.toFixed(2)}
                    addre={item.remark}
                    updateTime={item.updateTime}

                    onHandleDetail={this.handlerprops.bind(this, item)}

                  >
                  </Ddraworder>
                })
              }
              {
                footerStatus && <View className='b_drawback__more'>暂无更多</View>
              }
            </ScrollView>


          </View>
          {
            list.length === 0 && <DItemError></DItemError>
          }
        </View>
        <DModal
          isShow={isShow}
          title='撤回订单'
          text='确定要撤回订单吗？'
          firstbtn='取消'
          lastbtn='撤回'
          onFirst={this.onClose.bind(this)}
          onCancel={this.onClose.bind(this)}
          onLast={this.onLastHandle.bind(this)}
        ></DModal>

      </View>

    )
  }
}
